package com.woniu.utils.consumer;//package com.woniu.user.consumer;
//
//import cn.hutool.json.JSONUtil;
//
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//import java.util.List;
//
////消费者
//@Component
//@RabbitListener(queues = "order_queue")
//public class OrderNumQueueConsumer {
//    @Resource
//    private AlipayClient alipayClient;
//    @Resource
//    private OrderInfoMapper orderInfoMapper;
//    @Resource
//    private OrderItemMapper orderItemMapper;
//
//    @Resource
//    private BookInfoMapper bookInfoMapper;
//    @Transactional
//    @RabbitHandler
//    public void checkOrder(String orderNum) throws AlipayApiException {
//        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderNum+"\"}");
//        String result = alipayClient.execute(alipayRequest).getBody();
//        String response = JSONUtil.parseObj(result).get("alipay_trade_query_response").toString();
//        String payCode=JSONUtil.parseObj(response).get("code").toString();
//        if ("10000".equals(payCode)){
//            String payTime =JSONUtil.parseObj(response).get("send_pay_date").toString();
//            String status = JSONUtil.parseObj(response).get("trade_status").toString();
//            if ("TRADE_SUCCESS".equals(status)){
//                //支付成功 修改订单状态 和下单时间
//                OrderInfo orderInfo = orderInfoMapper.selectById(orderNum);
//                if (orderInfo.getOrderState()== StateData.ORDER_STATE_NOT_PAY){
//                    orderInfo.setOrderState(StateData.ORDER_STATE_PAYED);
//                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//                    LocalDateTime time = LocalDateTime.parse(payTime, formatter);
//                    orderInfo.setPayTime(time);
//
//                    //修改冻结量
//                    List<OrderItem> list = orderItemMapper.selectList(new QueryWrapper<OrderItem>().eq("order_id", orderNum));
//                    for (OrderItem item : list) {
//                        BookInfo book = bookInfoMapper.selectById(item.getBookId());
//                        book.setFreeze(book.getFreeze()-item.getBookCont());
//                        bookInfoMapper.updateById(book);
//                    }
//                    orderInfoMapper.updateById(orderInfo);
//                }
//            }
//        }else {
//            //用户未对订单付款 取消订单 还原库存
//            OrderInfo orderInfo = orderInfoMapper.selectById(orderNum);
//            orderInfo.setOrderState(StateData.ORDER_STATE_CANCEL);
//            orderInfoMapper.updateById(orderInfo);
//            List<OrderItem> list = orderItemMapper.selectList(new QueryWrapper<OrderItem>().eq("order_id", orderNum));
//            for (OrderItem item : list) {
//                BookInfo bookInfo = bookInfoMapper.selectById(item.getBookCont());
//                bookInfo.setNumber(bookInfo.getNumber()+item.getBookCont());
//                bookInfo.setFreeze(bookInfo.getFreeze()- item.getBookCont());
//                bookInfoMapper.updateById(bookInfo);
//            }
//        }
//    }
//}
